# -*- coding: utf-8 -*-
import sys
from resource_management import *
from resource_management.core.logger import Logger
from resource_management.libraries.functions import default
from resource_management.core.resources.system import Execute
from resource_management.libraries.functions.check_process_status import check_process_status
from resource_management.libraries.script.script import Script
import commands

reload(sys)
sys.setdefaultencoding('utf-8')

config = Script.get_config()

# package download url
presto_download_url = config['configurations']['ambari-presto-config']['presto.package.download.url']
presto_client_download_url = config['configurations']['ambari-presto-config']['presto_cli.package.download.url']

# hdp info
cmd = "/usr/bin/hdp-select versions"
hdp_version = commands.getoutput(cmd)
hdp_base_dir = '/usr/hdp/' + hdp_version
hostname = config['agentLevelParams']['hostname']

# presto info
presto_home = hdp_base_dir + "/presto"
presto_log_dir = "/var/log/presto"
presto_pid_dir = "/var/run/presto"
presto_etc_dir = presto_home + '/etc'
presto_etc_catalog_dir = presto_etc_dir + '/catalog'
presto_pid_file = "/var/run/presto/presto.pid"
presto_log_file = "/var/log/presto/presto.log"
presto_cli_file = presto_home + '/presto-cli.jar'
presto_data_dir = config['configurations']['presto-node']['node.data-dir']

# presto-env
presto_user = config['configurations']['presto-env']['presto_user']
presto_group = config['configurations']['presto-env']['presto_group']


# config.properties
presto_coordinator_config = {}
presto_coordinator_config.update(config['configurations']['presto-coordinator-config'])

http_server_http_port = config['configurations']['presto-coordinator-config']['http-server.http.port']
discovery_uri = config['configurations']['presto-coordinator-config']['discovery.uri']

presto_worker_config = {}
presto_worker_config.update(config['configurations']['presto-worker-config'])
presto_worker_config['http-server.http.port'] = http_server_http_port
presto_worker_config['discovery.uri'] = discovery_uri

# node.properties
presto_node_properties = {}
presto_node_properties.update(config['configurations']['presto-node'])
presto_node_properties['node_id'] = 'presto' + '_' + hostname


# jvm_config
presto_jvm_config = str(config['configurations']['presto-jvm']['jvm.config']).strip('\n')

# log.properties
presto_log_properties = {}
presto_log_properties.update(config['configurations']['presto-log'])

# connector.properties
presto_connector_properties = {}
presto_connector_properties.update(config['configurations']['presto-connector'])
